package com.ttt.gdfood.service;

import com.ttt.gdfood.entity.Image;
import com.ttt.gdfood.entity.Note;
import com.ttt.gdfood.mapper.ImageMapper;
import com.ttt.gdfood.mapper.NoteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NoteService {
    @Autowired
    NoteMapper noteMapper;

    @Autowired
    ImageMapper imageMapper;

    // 展示所有笔记
    public List<Note> noteShow(){
        return noteMapper.noteAll();
    }

    // 根据地区找笔记
    public List<Note> getNoteByRegion(String region){
        return noteMapper.noteByRegion(region);
    }

    // 根据用户id找笔记
    public List<Note> getNoteById(int id){
        return noteMapper.noteById(id);
    }

    // 根据笔记id找对应笔记
    public Note getNoteByNoteId(int id){
        return noteMapper.noteByNoteId(id);
    }

    // 根据关键字找笔记
    public List<Note> getNoteByKeyWord(String keyWord) {
        return noteMapper.noteByKeyWord(keyWord);
    }
    // 根据id删除
    public void deleteById(int id){
        noteMapper.deleteById(id);
    }

    // 添加笔记
    public void addNote(Note note){
        // 设置笔记默认属性
        note.setThumbs_up(0);
        note.setComment(0);
        note.setCollection(0);
        Date now = new Date();
        note.setCreate_time(now);
        note.setUpdate_time(now);

        noteMapper.addNote(note);

        List<Image> images = note.getImages();

        if(images!=null){
            for (Image img:images) {
                img.setSrc("http://8.142.20.59:9000/gdfood/"+img.getSrc());
                img.setNote_id(note.getId());
                imageMapper.addImage(img);
            }
        }
    }
}
